// Analysis for the state size paper
// LEC  Feb. 2, 2021
// Prepares new bidging data for R routines
// Rev. March 26, 2021

cd $OUTDIR


use analysis_data, clear



global TEX 1

if ($TEX) { 
	global FMAIN  "_cox_main_stata.tex, replace "
	global FEVENT "_cox_events_stata.tex, replace "
	global FEVENT2 "_cox_events2_stata.tex, replace "
	global FEVENT3 "_cox_events3_stata.tex, replace "
	global FOLS "_ols_main.tex, replace "
	global FPOST "_cox_post45.tex, replace "
	global WIMFEIN "_cox_wimfein.tex, replace "
	global POST1886MAIN "_cox_post1886main.tex, replace "
	global POST1886EVENT "_cox_post1886event.tex, replace "
	

} 
else {
	global FMAIN "_tab.txt, replace "
	global FEVENT "_tab.txt, append "
	global FEVENT2 "_tab.txt, append "
	global FEVENT3 "_tab.txt, append "
	global FOLS "_tab.txt, append "
	global FPOST "_tab.txt, append "
	global WIMFEIN "_tab.txt, append "
	global POST1886MAIN "_tab.txt, append "
	global POST1886EVENT "_tab.txt, append "
}

cd $OUTPUTDIR

label variable lnetcgain "Net terr. gain, log"
label variable lef "Ethnic frac."
label variable lagfrac "Terr. frac."
label variable llarea "State area, log"
label variable llage "State age, log"
label variable llelevmean "Elev. mean, log"
label variable llelevsd "Elev. SD, log"
label variable llpop "State population, log"
label variable lcumulethsec "# eth. secession"
label variable lcumulnethsec "# non-eth. secession"
label variable lcumulethabsorb "# unific."
label variable lcumulnethabsorb "# conquests"

xtset id year
gen period = int(year/10)

gen larea = l.area

gen ww = 0
replace ww = 1 if year>=1915 & year <=1918
replace ww = 1 if year>=1940 & year <=1945


gen gain1 = 0
replace gain1 = 1 if netgain>0

gen loss1 = 0
replace loss1 = 1 if netgain<0



// continuous DV for gain and loss

gen lngain = .
replace lngain = log(netgain+1) 
replace lngain = 0 if netgain < 0 & netgain!=.

gen lnloss = .
replace lnloss = log(abs(netgain)+1) 
replace lnloss = 0 if netgain > 0 & netgain!=.

gen event = .
replace event = 0 if netgain==0
replace event = 1 if netgain!=0 & netgain!=.

btscs event year idc, gen(dur) nspline(3)
rename _spline1  espline1
rename _spline2  espline2
rename _spline3  espline3



// DEFINE SPELLS

xtset idc year
gen spellonset = 0
replace spellonset = 1 if l.area!=l2.area
bys idc (year): gen spellnum = sum(spellonset)
gen idstr = string(idc)+"."+string(spellnum)
egen spell = group(idstr)

bys spell: egen minspellyear = min(year)
gen spellyear = year - minspellyear + 1



//// event models, first define dichtomous DVs

gen absorb1 = 0
replace absorb1 = 1 if absorba>0 & absorba!=.

gen absorb1b = 0
replace absorb1b = 1 if absorbb>0 & absorbb!=.

gen unif1 = 0
replace unif1 = 1 if ethabsorba>0 & ethabsorba!=.

gen sec1 = 0
replace sec1 = 1 if secessionb>0 & secessionb!=.
replace sec1 = 1 if collapseb>0 & collapseb!=.


gen ethsec1 = 0
replace ethsec1 = 1 if ethsecb>0 & ethsecb!=.
replace ethsec1 = 1 if ethcollapseb>0 & ethcollapseb!=.
 
gen transf1 = 0
replace transf1 = 1 if transfera>0 & transfera!=.

gen irr1 = 0
replace irr1 = 1 if irra>0 & irra!=. 


gen unif1b = 0
replace unif1b = 1 if ethabsorbb>0 & ethabsorbb!=.

gen irr1b = 0
replace irr1b = 1 if irrb>0 & irrb!=.

// Defining duration variables

btscs ethsec1 year idc, gen(dur_ethsec) nspline(3)
rename _spline1  spline1ethsec
rename _spline2  spline2ethsec
rename _spline3  spline3ethsec

btscs unif1 year idc, gen(dur_unif) nspline(3)
rename _spline1  spline1unif
rename _spline2  spline2unif
rename _spline3  spline3unif

btscs irr1 year idc, gen(dur_irr) nspline(3)
rename _spline1  spline1irr
rename _spline2  spline2irr
rename _spline3  spline3irr



bys idc (year): gen lcumulloss = sum(l.loss1)
gen losshist1 = 0
replace losshist1 = 1 if lcumulloss > 0 & lcumulloss != .

bys idc (year): gen lcumulgain = sum(l.gain1)
gen gainhist1 = 0
replace gainhist1 = 1 if lcumulgain > 0 & lcumulgain != .

gen ethsechist1 = 0
replace ethsechist1 = 1 if lcumulethsec > 0 & lcumulethsec != .

gen unifhist1 = 0
replace unifhist1 = 1 if lcumulethabsorb > 0 & lcumulethabsorb != .

gen irrhist1 = 0
replace irrhist1 = 1 if lcumulirr > 0 & lcumulirr != .


// DURATION MODELS: EUROPEAN DATA

// PREPARE TABLE 3: MAIN GAIN AND LOSS MODELS
// PREPARE TABLE A13: RUN WITH CENTENNIA DATA

eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

/* NOT USED CAN BE DELETED
 gen sample2 = e(sample)
stset spellyear, id(spell) failure(loss1==1) scale(1) 
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
 margins, pred(xb) at(lef=(0(0.05)1) lagfrac=0 llage=3.6 llpop=14.6 llarea=10.4  llelevmean=5.688389 llelevsd=5.06 spellnum=4)
 marginsplot, xlab(0(0.1)1) recast(line) recastci(rarea)

 stset spellyear, id(spell) failure(gain1==1) scale(1) 
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
 margins, predict(xb) at(lagfrac=(0(0.05)1) lef=0.16 llage=3.4 llpop=14.22 llarea=10.16  llelevmean=5.688389 llelevsd=4.955033)
 marginsplot, xlab(0(0.1)1) recast(line) recastci(rarea)
*/

// OUTPUT TABLE 3: BRDG_COX_MAIN_STATA.TEX
// OUTPUT TABLE A13:  cent_cox_main_stata.tex (WITH CENTENNIA DATA)
esttab m1 m2 m3 m4 using $PREFIX$FMAIN b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, Europe 1816-2017, Cox proportional hazard models\label{tab:cox:main:$PREFIX}) ///
   scalars("strat Strata")

   
//   OUTPUT TABLE A1: brdg_cox_main_stata_full  WITH ALL CONTROLS
	  esttab m1 m2 m3 m4 using brdg_cox_main_stata_full.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac llage llarea llpop llelevmean llelevsd) ///
 keep(lef lagfrac llage llarea llpop llelevmean llelevsd) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models\label{tab:cox:main:$PREFIX:full}) ///
      scalars("strat Strata")


	  
	   
// PREPARE TABLE 7: ETHNIC EVENTS
// PREPARE TABLE A14: SAME WITH CENNTENNIA DATA

eststo clear   

stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m4

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// OUTPUT TABLE 7: brdg_cox_events_stata.tex
// OUTPUT TABLE A14:  cent_cox_events_stata.tex (WITH CENTENNIA DATA)
esttab m1 m2 m3 m4 m5 m6 using $PREFIX$FEVENT b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
   mtitles("Eth. Secession" "Eth. Secession" "Unification" "Unification" "Irredentism" "Irredentism") ///
   title(Border-change events, Europe 1816-2017, Cox proportional hazard models \label{tab:cox:events:$PREFIX}) ///
   scalars("strat Strata")
 

 
// PREPARE TABLE THAT IS NOT USED 

eststo clear   

stset spellyear, id(spell) failure(unif1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1

stset spellyear, id(spell) failure(unif1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(irr1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(irr1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$FEVENT2 b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
    mtitles("Unification" "Unification" "Irredentism" "Irredentism") ///
   title(Ethnic border-change events (State B), Europe 1816-2017, Cox proportional hazard models \label{tab:cox:events2:$PREFIX}) ///
      scalars("strat Strata")
 
 
// PREPARE TABLE A3: ALL EVENTS
 
eststo clear   

stset spellyear, id(spell) failure(sec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc)  efron
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(sec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(absorb1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(absorb1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

stset spellyear, id(spell) failure(transf1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(transf1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)
estadd local strat "Yes"
estimates store m6

// OUTPUT TABLE A3:  brdg_cox_events3_stata.tex
esttab m1 m2 m3 m4 m5 m6 using $PREFIX$FEVENT3 b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
    mtitles("Secession" "Secession" "Absorption" "Absorption" "Irredentism" "Irredentism") ///
   title(All border-change events, Europe 1816-2017, Cox proportional hazard models \label{tab:cox:events3:$PREFIX}) ///
       scalars("strat Strata")  
   
   
 
 
 
// PREPARE TABLE A5: OLS MODELS
 
eststo clear

reghdfe lnloss lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "No"
estimates store m1

reghdfe lnloss lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year idc) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "Yes"
estimates store m2

reghdfe lngain lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "No"
estimates store m3

reghdfe lngain lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year idc) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "Yes"
    estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$FOLS b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Log Losses and gains, Europe 1816-2017, OLS \label{tab:ols:main:$PREFIX}) ///
   scalars("geocontrols Controls" "yearfe Year FE" "statefe State FE")
 
exit

// MAIN DURATION MODELS: PRE-/POST 1945 split
if("$PREFIX" == "cshpgreg") {
    // Make post45 var
    gen post45 = 0
    replace post45 = 1 if year > 1945
    lab var post45 "Post-45 (0/1)"
    gen lef45 = post45 * lef
    lab var lef45 "Ethnic frac. x post-45"
    gen lagfrac45 = post45 * lagfrac
    lab var lagfrac45 "Terr. frac. x post-45"

    // Estimation
    eststo clear

    stset spellyear, id(spell) failure(loss1==1) scale(1)
    stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
    estadd local strat "No"
    estimates store m1

    stset spellyear, id(spell) failure(loss1==1) scale(1)
    stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
    estadd local strat "Yes"
    estimates store m2


    stset spellyear, id(spell) failure(gain1==1) scale(1)
    stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
    estadd local strat "No"
    estimates store m3

    stset spellyear, id(spell) failure(gain1==1) scale(1)
    stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
    estadd local strat "Yes"
    estimates store m4

    esttab m1 m2 m3 m4 using $PREFIX$FPOST b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
     order(lef lagfrac post45 lef45 lagfrac45) ///
     keep(lef lagfrac post45 lef45 lagfrac45) ///
      mtitles("Loss" "Loss" "Gain" "Gain") ///
       title(Losses and gains, Europe 1816-2017, Cox proportional hazard models: Pre- vs. post-1945\label{tab:cox:post45:$PREFIX}) ///
       scalars("strat Strata")
}
   
 
   
   
   
// MAIN DURATION MODELS: NATIONALIST ORGS from Wimmer & Feinstein
 /*
// Make interaction vars

lab var natorgyesno "Nationalist org."
gen lefnatorg = natorgyesno * lef
lab var lefnatorg "Ethnic frac. x nat. org."
gen lagnatorg = natorgyesno * lagfrac
lab var lagnatorg "Terr. frac. x nat. org."

// Estimation
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$WIMFEIN b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac natorgyesno lefnatorg lagnatorg) ///
 keep(lef lagfrac natorgyesno lefnatorg lagnatorg) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, Europe 1816-2017, Cox proportional hazard models: Nationalist Organizations\label{tab:cox:wimfein:$PREFIX}) ///
   scalars("strat Strata")
 */  

 // PREPARE TABLE A9
// THIS TABLE NEEDS TO BE RUN WITH FIXED HEG DATA
// PREPARE TABLE A15: LOSS AND GAIN MODELS, POST-1886 CASES ONLY

eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

// OUTPUT TABLE A9:   brdgfix_cox_post1886main.tex

esttab m1 m2 m3 m4 using $PREFIX$POST1886MAIN b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, Europe 1886-2017, Cox proportional hazard models\label{tab:cox:post1886main:$PREFIX}) ///
   scalars("strat Strata")
   

// PREPARE TABLE A10
// POST 1886 Event data
   
eststo clear   

stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m2


/* No unification estimates possilbe, since no cases
stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m4
*/
stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year >= 1886, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// OUTPUT TABLE A10:  brdgfix_cox_post1886event.tex
esttab m1 m2 m5 m6 using $PREFIX$POST1886EVENT b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
   mtitles("Eth. Secession" "Eth. Secession" "Irredentism" "Irredentism") ///
   title(Border-change events, Europe 1886-2017, Cox proportional hazard models \label{tab:cox:post1886events:$PREFIX}) ///
   scalars("strat Strata")
 

// PREPARE TABLE A7: MAIN GAIN AND LOSS MODELS WHILE REMOVING RUSSIA/USSR

eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if id!=365, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if id!=365, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if id!=365, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLSS if id!=365, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

// OUTPUT TABLE A7: temp.tex file name (placeholder)
esttab m1 m2 m3 m4 using temp.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, Europe 1816-2017 without Russia/USSR, Cox proportional hazard models\label{tab:cox:main:$PREFIX}) ///
      scalars("strat Strata")  
  
  
// PREPARE TABLE A9: DROPPING PRE-1886  
// October 2022 restricting models by dropping pre-1886 cases (used in Model A9 with frozen ethnicity data)
if("$PREFIX" == "brdgfix") {
    eststo clear

    stset spellyear, id(spell) failure(loss1==1) scale(1)
    stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) efron
    estadd local strat "No"
    estimates store m1

    stset spellyear, id(spell) failure(loss1==1) scale(1)
    stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) strata(year) efron
    estadd local strat "Yes"
    estimates store m2


    stset spellyear, id(spell) failure(gain1==1) scale(1)
    stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) efron
    estadd local strat "No"
    estimates store m3

    stset spellyear, id(spell) failure(gain1==1) scale(1)
    stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) strata(year) efron
    estadd local strat "Yes"
    estimates store m4

    /*
     gen sample2 = e(sample)
    stset spellyear, id(spell) failure(loss1==1) scale(1)
    stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
     margins, pred(xb) at(lef=(0(0.05)1) lagfrac=0 llage=3.6 llpop=14.6 llarea=10.4  llelevmean=5.688389 llelevsd=5.06 spellnum=4)
     marginsplot, xlab(0(0.1)1) recast(line) recastci(rarea)

     stset spellyear, id(spell) failure(gain1==1) scale(1)
    stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
     margins, predict(xb) at(lagfrac=(0(0.05)1) lef=0.16 llage=3.4 llpop=14.22 llarea=10.16  llelevmean=5.688389 llelevsd=4.955033)
     marginsplot, xlab(0(0.1)1) recast(line) recastci(rarea)
    */

    // OUTPUT TABLE A9
     esttab m1 m2 m3 m4 using temp.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
     order(lef lagfrac ) /// llarea llpop llelevmean llelevsd) ///
     keep(lef lagfrac ) /// llarea llpop llelevmean llelevsd) ///
     mtitles("Loss" "Loss" "Gain" "Gain") ///
       title(Losses and gains, Europe 1886-2017, Cox proportional hazard models(fixed HEG data) \label{tab:cox:main:$PREFIX:full}) ///
          scalars("strat Strata")
}
	  
eststo clear   

stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m4

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum if year>=1886, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// esttab m1 m2 m3 m4 m5 m6 using brdg_tab.txt /*brdg_cox_events_stata.txt*/, append b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
esttab m1 m2 m3 m4 m5 m6 using temp.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
   mtitles("Eth. Secession" "Eth. Secession" "Unification" "Unification" "Irredentism" "Irredentism") ///
   title(Border-change events, Europe 1886-2017, Cox proportional hazard models (fixed HEG data) \label{tab:cox:events:$PREFIX}) ///
   scalars("strat Strata")
 
 
 

 // PREPARE FIGURE A20
 // MAIN DURATION MODELS: PRE-/POST 1945 split
 // 
 
// Make post45 var


gen post45 = 0
replace post45 = 1 if year > 1945
lab var post45 "Post-45 (0/1)"
gen lef45 = post45 * lef
lab var lef45 "Ethnic frac. x post-45"
gen lagfrac45 = post45 * lagfrac
lab var lagfrac45 "Terr. frac. x post-45"
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLSS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

// OUTPUT TABLE A20  brdg_cox_post45.tex
esttab m1 m2 m3 m4 using $PREFIX$FPOST b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac post45 lef45 lagfrac45) ///
 keep(lef lagfrac post45 lef45 lagfrac45) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models: Pre- vs. post-1945\label{tab:cox:post45:$PREFIX}) ///
      scalars("strat Strata")
   

 // NOT SHOWN IN PAPER OR APPENDIX:
 // Testing quadratic term
 
 gen lef2 = lef*lef
 
 eststo clear   

stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lef2 lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lef2 lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lef2 lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lef2 lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m4

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lef2 lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lef2 lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// esttab m1 m2 m3 m4 m5 m6 using brdg_tab.txt /*brdg_cox_events_stata.txt*/, append b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
esttab m1 m2 m3 m4 m5 m6 using temp.txt, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lef2 lagfrac) ///
 keep(lef lef2 lagfrac) ///
   mtitles("Eth. Secession" "Eth. Secession" "Unification" "Unification" "Irredentism" "Irredentism") ///
   title(Border-change events, Europe 1816-2017, Cox proportional hazard models \label{tab:cox:events:$PREFIX}) ///
   scalars("strat Strata")
 
